package com.mccken.designpattern.adapter.adapter2.step3;

import com.mccken.designpattern.adapter.adapter2.step1.SigninService;
import com.mccken.designpattern.adapter.adapter2.support.ResultMsg;

import lombok.SneakyThrows;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2022-10-26 12:00
 **/
public class PassportForThirdAdapter extends SigninService implements IPassportForThird {
	
	private LoginForQQAdapter loginForQQAdapter = new LoginForQQAdapter();
	private LoginForWechatAdapter loginForWechatAdapter = new LoginForWechatAdapter();
	private LoginForTelAdapter loginForTelAdapter = new LoginForTelAdapter();
	private LoginForTokenAdapter loginForTokenAdapter = new LoginForTokenAdapter();
	private LoginForSinaAdapter loginForSinaAdapter = new LoginForSinaAdapter();
	
	@Override
	public ResultMsg loginForQQ(String id) {
		return this.processLogin(id, loginForQQAdapter);
	}

	@Override
	public ResultMsg loginForWechat(String id) {
		return this.processLogin(id, loginForWechatAdapter);
	}

	@Override
	public ResultMsg loginForTelephone(String telephone, String code) {
//		docheckCode(telephone, code)
		return this.processLogin(telephone, loginForTelAdapter);
	}

	@Override
	public ResultMsg loginForToken(String token) {
		return this.processLogin(token, loginForTokenAdapter);
	}

	@Override
	public ResultMsg loginForRegistry(String username, String passport) {
		super.register(username,passport);
		return super.login(username, passport);
	}

	/**
	 * 这里用到了简单工厂模式和策略模式
	 * 
	 * @param key
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	@SneakyThrows
	private ResultMsg processLogin(String key, LoginAdapter loginAdapter) {
		if (loginAdapter.support(loginAdapter)) {
			return loginAdapter.login(key, loginAdapter);	
		}
		return null;
	}
}
